{extend name="layout:base" /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="test-table-reload-btn">
                <div class="layui-inline">
                    <button id="btnAdd" class="layui-btn layui-btn-sm layuiadmin-btn-list" data-type="add">添加员工</button>
                </div>
                <div class="layui-inline">
                    <input class="layui-input" name="keywords" id="keywords" autocomplete="off">
                </div>
                <button class="layui-btn" data-type="reload">搜索</button>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="tbList" class="layui-table" lay-filter="tbList" ></table>
            <script type="text/html" id="state">
                <input type="checkbox" name="state" value="{{d.id}}" lay-skin="switch" lay-text="在职|离职" lay-filter="state" {{ d.state== 0 ? 'checked' : '' }}>
            </script>
            <script type="text/html" id="op">

                <a class="layui-btn layui-btn-xs"  lay-href="{:url('edit')}?id={{d.id}}"  lay-text="编辑-{{d.username}}" lay-tips="编辑-{{d.username}}">编</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reset" lay-tips="密码重置-{{d.username}}">密</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" lay-tips="删除员工-{{d.username}}">删</a>
                <a class="layui-btn layui-btn-xs layui-btn-primary" href="/{{d.login}}/" target="_blank" lay-tips="登录后台-{{d.username}}">登录</a>


            </script>
        </div>
    </div>
</div>

{/block}

{block name="script"}
<script>
    layui.config({
        base: '__STATIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'util','form'], function () {

        //表格
        var $ = layui.$,form = layui.form,layer = layui.layer,table = layui.table;
        table.render({
            elem: '#tbList',
            url: "{:url('getList')}",
            cellMinWidth: 80,
            cols: [[
                {field: 'id', fixed: 'left', title: 'ID',width:60},
                {field: 'username',fixed: 'left',title: '姓名',width:100},
                {field: 'role_title',title: '角色',width:100},
                {field: 'phone',  title: '手机'},
                {field: 'email',  title: '邮箱'},
                {field: 'qq',  title: 'qq'},
                {field: 'remarks',  title: '备注', edit: 'text'},
                {field: 'create_time',  title: '注册时间'},
                {field: 'state', title: '状态',width:100, templet: '#state'},
                {fixed: 'right', title: '操作',width:200, toolbar: '#op'}
            ]],
            page: true,
            limit:15
        });

        // 监听工具条
        table.on('tool(tbList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除', {icon: 3, title: '提示'}, function (index) {
                    $.post("{:url('del')}", {id: data['id']}, function (result) {
                        if (result['code'] == 0) {
                            layer.msg(result['msg'], {icon: 1, time: 2000}, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                var url = "{:url('edit')}?id=" + data['id'];
                var index = layer.open({
                    type: 2,
                    title: '修改内容',
                    area: ['90%', '90%'],
                    shade: 0,
                    maxmin: true,
                    content: url,
                    zIndex: layer.zIndex
                });
            }else if (obj.event === 'reset') {
                layer.confirm('确定重置密码?', {icon: 3, title: '提示'}, function (index) {
                    $.post("{:url('reset')}", {id: data['id']}, function (result) {
                        if (result['code'] == 0) {
                            layer.msg(result['msg'], {icon: 1, time: 2000}, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                });
            }
        });
        //监听单元格编辑
        table.on('edit(tbList)', function (obj) {
            var value = obj.value; //得到修改后的值
            var data = obj.data; //得到所在行所有键值
            var field = obj.field; //得到字段
            layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);

            $.post("{:url('state')}", {id: data.id, remarks: value}, function (result) {
                if (result['code'] == 0) {
                    layer.msg(result['msg'], {icon: 1, time: 1000}, function () {
                        location.reload();
                    });
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });



        //监听推荐

        form.on('switch(state)', function (obj) {
            var data = {id: this.value, 'state': obj.elem.checked ? 0 : 1};
            $.post("{:url('state')}", data, function (result) {
                if (result['code'] == 0) {
                    location.reload();
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });

        form.on('switch(webPower)', function (obj) {
            var data = {id: this.value, 'webPower': obj.elem.checked ? 1 : 0};
            $.post("{:url('webPower')}", data, function (result) {
                if (result['code'] == 0) {
                    location.reload();
                } else {
                    layer.msg(result['msg'], {icon: 3, time: 3000});
                }
            });
        });


        //操作
        var $ = layui.$, active = {
            add: function () {
                var index = layer.open({
                    type: 2,
                    title: '添加内容',
                    area: ['90%','90%'],
                    shade: 0,
                    maxmin: true,
                    content: "{:url('add')}",
                    zIndex: layer.zIndex
                });
            },
            reload: function(){
                var dID=$('#departID').val();
                var keywords=$('#keywords').val();

                //执行重载
                table.reload('tbList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        departID: dID
                        ,keywords: keywords
                    }
                });

            }
        };


        $('.test-table-reload-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
{/block}